﻿using BW.WMS.CodeEnum;
using BW.WMS.Model;
using BW.WMS.StockControl_GJH.Dto.Output;
using BW.WMS.StoreManagement_XKY.DTO.Output;
using BW.WMS.XKY;
using MLModel_ConsoleApp;
using NPOI;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Asn1.Mozilla;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace BW.WMS.StoreManagement_XKY
{
    public class InventoryRecordsAppService : ApplicationService, IInventoryRecordsAppService
    {
        private readonly IRepository<UserModel> _userRepository;
        private readonly IRepository<Me_StHd, Guid> _sthdRepository;
        private readonly IRepository<Dm_Unit, Guid>  _unitRepository;
        private readonly IRepository<Dm_Ent, Guid> _entRepository;
        private readonly IRepository<Dm_Client, Guid> _clientRepository;

        public InventoryRecordsAppService(IRepository<UserModel> userRepository, IRepository<Me_StHd, Guid> sthdRepository, IRepository<Dm_Item, Guid> itemRepository, IRepository<Dm_ItemType, Guid> itemtypeRepository, IRepository<Dm_Code, Guid> codeRepository, IRepository<Dm_Librarylocation, Guid> librarylocationRepository, IRepository<Dm_Warehouser, Guid> warehouserRepository, IRepository<Dm_Unit, Guid> unitRrepository, IRepository<Dm_Ent, Guid> entRepository, IRepository<Dm_Client, Guid> clientRepository, IRepository<Me_StSub, Guid> stsubRepository)
        {
            _userRepository = userRepository;
            _sthdRepository = sthdRepository;
            _itemRepository = itemRepository;
            _itemtypeRepository = itemtypeRepository;
            _codeRepository = codeRepository;
            _librarylocationRepository = librarylocationRepository;
            _WarehouserRepository = warehouserRepository;
            _unitRepository = unitRrepository;
            _entRepository = entRepository;
            _clientRepository = clientRepository;
            _stsubRepository = stsubRepository;
        }

        private readonly IRepository<Me_StSub, Guid> _stsubRepository;
        private readonly IRepository<Dm_Item, Guid> _itemRepository;
        private readonly IRepository<Dm_ItemType, Guid> _itemtypeRepository;
        private readonly IRepository<Dm_Code, Guid> _codeRepository;
        private readonly IRepository<Dm_Librarylocation, Guid> _librarylocationRepository;
        private readonly IRepository<Dm_Warehouser, Guid> _WarehouserRepository;
        public async Task<ApiPaging<InventoryRecordsOutput>> GetList(string date, string? Inoutid, string? Typeid, string? Warehouseid, string? Itemid, string? Itemtypeid)
        {
            var Stud =await _sthdRepository.GetQueryableAsync();//
            var DtSub=await _stsubRepository.GetQueryableAsync();//
            var Item=await _itemRepository.GetQueryableAsync();//
            var ItemType=await _itemtypeRepository.GetQueryableAsync();//
            var Warehouse=await _WarehouserRepository.GetQueryableAsync();//
            var Librarylocation=await _librarylocationRepository.GetQueryableAsync();//
            //var Purchase=await _PurchaseHdRepository.GetQueryableAsync();
            var User=await _userRepository.GetQueryableAsync();//
            var Unit=await _unitRepository.GetQueryableAsync();//
            var Ent=await _entRepository.GetQueryableAsync();//
            var Client=await _clientRepository.GetQueryableAsync();//
            var code=await _codeRepository.GetQueryableAsync();
            //筛选
            var dates=date.Split(',');
            if (dates.Length > 1)
            {
                Stud = Stud.Where(x => x.Inoutdate.Date >= Convert.ToDateTime(dates[0]).Date && x.Inoutdate.Date <= Convert.ToDateTime(dates[1]).Date);
            }
            else 
            {
                Stud = Stud.Where(x => x.Inoutdate.Date == Convert.ToDateTime(date).Date);
            }

            if (!string.IsNullOrEmpty(Inoutid)) 
            {
                Stud = Stud.Where(x => x.Inoutid.Contains(Inoutid));
            }
            if (!string.IsNullOrEmpty(Typeid)&&Typeid!="undefined") 
            {
                Stud = Stud.Where(x => x.Typeid==Typeid);
            }
            if (!string.IsNullOrEmpty(Warehouseid)&&Warehouseid != "undefined") 
            {
                Stud = Stud.Where(x => x.Warehouseid==Warehouseid);
            }
            if (!string.IsNullOrEmpty(Itemid)) 
            {
                DtSub = DtSub.Where(x => x.Itemid.Contains(Itemid));
            }
            if (!string.IsNullOrEmpty(Itemtypeid)&&Itemtypeid != "undefined") 
            {
                Item = Item.Where(x => x.Itemtypeid==Itemtypeid);
            }

            var listdata = from a in Stud
                           join b in DtSub on a.Inoutid equals b.Inoutid
                           join c in Item on b.Itemid equals c.Itemid
                           join d in ItemType on c.Itemtypeid equals d.Itemtypeid
                           join e in Warehouse on a.Warehouseid equals e.Warehouseid
                           join f in Librarylocation on a.Librarylocationid equals f.Librarylocationid
                           join g in Unit on b.Unitid equals g.Unitid
                           join h in Ent on c.Entid equals h.Entid
                           join j in User on a.Handempid equals j.Id.ToString()
                           join k in code on a.Typeid equals k.Typeid
                           select new InventoryRecordsOutput2
                           {                    
                              Direction=a.Direction,
                              Inoutid=b.Inoutid,
                              Inoutdate=a.Inoutdate,
                              TypeName=k.Typename,
                              WarehouseName=e.Warehousename,
                              LibrarylocationName=f.Librarylocationname,
                              Itemid=c.Itemid,
                              Itemname=c.Itemname,
                              UnitName=g.Unitname,
                              Itemtypename=d.Itemtypename,
                              Quantity=b.Quantity,
                              Prices=b.Prices,
                              Amount=b.Amount,
                              HandempName=j.UserName,
                              Itemspec=c.Itemspec
                           };

            
            var list = new InventoryRecordsOutput() 
            {
                CNum=listdata.Where(x=>x.Direction==2).Count(),
                CAmount=listdata.Where(x=>x.Direction==2).Sum(x=>Convert.ToInt32(x.Amount)).ToString(),
                CQuantity=listdata.Where(x=>x.Direction==2).Sum(x=>Convert.ToInt32(x.Quantity)),
                RNum=listdata.Where(x=>x.Direction==1).Count(),
                RAmount=listdata.Where(x=>x.Direction==1).Sum(x=>Convert.ToInt32(x.Amount)).ToString(),
                RQuantity=listdata.Where(x=>x.Direction==1).Sum(x=>Convert.ToInt32(x.Quantity)),
                Output=listdata.ToList()
            };

            return new ApiPaging<InventoryRecordsOutput>()
            {
                TotalCount = list.Output.Count(),
                Data = list
            };

        }

        public Task<ApiResult<string>> Mlnet()
        {
            MLModel.ModelInput sampleData = new MLModel.ModelInput()
            {
                Year = 2023F,
                Month = 1F,
                Day = 28F,
            };
            // MLModel功能暂时注释掉，因为MLModel类型在当前项目中不存在
            // TODO: 如果需要ML功能，请添加相应的MLModel引用
            var predictionResult = MLModel.Predict(sampleData);
            return Task.FromResult(new ApiResult<string>()
            {
                Code = ApiEnum.Success,
                Message = "成功",
                Data = Convert.ToString(predictionResult.Score)
            });
        }


    }
}
